//
//  MessageViewController.m
//  XMLTest
//
//  Created by Adiljan on 2019/2/17.
//  Copyright © 2019年 Adiljan. All rights reserved.
//

#import "MessageViewController.h"
#import "MessageModel.h"

@interface MessageViewController ()<NSXMLParserDelegate,UITableViewDataSource,UITableViewDelegate>
//@property (strong, nonatomic) NSMutableArray *teaArray;
@property (strong, nonatomic) MessageModel *Tmodel;
@property (strong, nonatomic) NSString *stringData;
@property (strong, nonatomic) NSMutableArray <MessageModelPuList *> *PuList;

@property (strong, nonatomic) UITableView *tblXml;
@end

@implementation MessageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSLog(@"当前页 ===== MessageViewController");
    
    self.tblXml = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height) style:UITableViewStylePlain];
    [self.view addSubview:self.tblXml];
    self.tblXml.dataSource = self;
    self.tblXml.delegate = self;
    
    
    //    NSString *filePath =  [[NSBundle mainBundle] pathForResource:@"test" ofType:@"xml"];
    
    NSString *filePath =  [[NSBundle mainBundle] pathForResource:@"Message" ofType:@"xml"];
    
    NSURL *url = [[NSURL alloc]initFileURLWithPath:filePath];
    NSXMLParser *par = [[NSXMLParser alloc]initWithContentsOfURL:url];
    par.delegate = self;
    [par parse];
}

//- (NSMutableArray *)teaArray{
//    if (!_teaArray) {
//        _teaArray = [NSMutableArray array];
//    }
//    return _teaArray;
//}

//2.-- NSXMLParserDelegate  代理方法
#pragma mark -- NSXMLParserDelegate

- (void)parserDidStartDocument:(NSXMLParser *)parser{
    NSLog(@"xml开始解析");
    
    //    self.teaArray = [NSMutableArray array];
    
}
- (void)parserDidEndDocument:(NSXMLParser *)parser{
    NSLog(@"结束解析");
    
}
- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary<NSString *,NSString *> *)attributeDict{
    NSLog(@"开始标签：%@",elementName);
    
        if ([elementName isEqualToString:@"PuList"]) {
            self.Tmodel = [MessageModel new];
            self.PuList = [NSMutableArray <MessageModelPuList *> new];
        }
    
}
- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName{
    NSLog(@"结束标签：%@",elementName);
    if ([elementName isEqualToString:@"PU"]) {
        MessageModelPuList *plist = [MessageModelPuList new];
        plist.PU = self.stringData;
        [self.PuList addObject:plist];
    }else if ([elementName isEqualToString:@"PuList"]){
        self.Tmodel.PuList = self.PuList;
        [self.tblXml reloadData];
    }
    
    NSLog(@"teaArray: %@",self.Tmodel.PuList);
}
- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string{
    NSLog(@"内容是：%@",string);
    self.stringData = string;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
   return self.Tmodel.PuList.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *identifier = @"mycell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:identifier];
    }
    
    cell.textLabel.text = self.PuList[indexPath.row].PU;
    //config the cell
    return cell;
}

@end
